package com.tide.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.tide.crm.dao.CustomerEventDao;
import com.tide.crm.dto.CustomContactStakeholderDto;
import com.tide.crm.dto.CustomerDto;
import com.tide.crm.model.CustomerContacts;
import com.tide.crm.model.CustomerEvent;
import com.tide.crm.model.CustomerInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.tide.crm.service.CustomContactStakeholderService;
import org.springframework.stereotype.Service;
import com.tide.crm.dao.CustomContactStakeholderDao;
import com.tide.crm.model.CustomContactStakeholder;
import org.springframework.transaction.annotation.Transactional;

@Service("customContactStakeholderService")
public class CustomContactStakeholderServiceImpl implements CustomContactStakeholderService {

    @Autowired
    private CustomContactStakeholderDao customContactStakeholderDao;

    @Autowired
    private CustomerEventDao customerEventDao;

    @Override
    public CustomContactStakeholder save(CustomContactStakeholderDto customContactStakeholderDto,String[] eventArray,String[] eventDateArray) {
        CustomContactStakeholder customContactStakeholder = new CustomContactStakeholder();
        BeanUtils.copyProperties(customContactStakeholderDto,customContactStakeholder);
        customContactStakeholderDao.save(customContactStakeholder);
        int index = 0;
        if (eventArray.length > 0) {
            for (String event : eventArray) {
                CustomerEvent customerEvent = new CustomerEvent();
                customerEvent.setCreater(customContactStakeholderDto.getCreater());
                customerEvent.setCreateDate(customContactStakeholderDto.getCreateDate());
                customerEvent.setModifyer(customContactStakeholderDto.getModifyer());
                customerEvent.setModifyDate(customContactStakeholderDto.getModifyDate());
                customerEvent.setContactId(customContactStakeholder.getId());
                customerEvent.setType("2");
                customerEvent.setEvent(event);
                customerEvent.setEventDate(eventDateArray[index]);
                customerEventDao.save(customerEvent);
                index++;
            }
        }
        return customContactStakeholder;
    }

    @Override
    public CustomContactStakeholderDto getById(Long id) {
        CustomContactStakeholderDto stakeholderDto = new CustomContactStakeholderDto();
        CustomContactStakeholder stakeholder = customContactStakeholderDao.getById(id);
        BeanUtils.copyProperties(stakeholder,stakeholderDto);
        List<CustomerEvent> eventList = customerEventDao.findListByHolderId(id);
        String event[] = new String[eventList.size()];
        String eventDate[] = new String[eventList.size()];
        if (!eventList.isEmpty()){
            int index = 0;
            for (CustomerEvent customerEvent : eventList) {
                event[index] = customerEvent.getEvent();
                eventDate[index] = customerEvent.getEventDate();
                index++;
            }
        }
        stakeholderDto.setEvent(event);
        stakeholderDto.setEventDate(eventDate);
//        stakeholderDto.setEventList(eventList);
        return stakeholderDto;
    }

    @Override
    public CustomContactStakeholderDto update(CustomContactStakeholderDto stakeholderDto,String[] eventArray,String[] eventDateArray) {
        CustomContactStakeholder stakeholder = new CustomContactStakeholder();
        BeanUtils.copyProperties(stakeholderDto,stakeholder);
        customContactStakeholderDao.update(stakeholder);
        customerEventDao.deleteEventByContactId(stakeholderDto.getId());
        if (eventArray.length > 0) {
            int index = 0;
            for (String event : eventArray) {
                CustomerEvent customerEvent = new CustomerEvent();
                customerEvent.setCreater(stakeholderDto.getCreater());
                customerEvent.setCreateDate(stakeholderDto.getModifyDate());
                customerEvent.setModifyer(stakeholderDto.getModifyer());
                customerEvent.setModifyDate(stakeholderDto.getModifyDate());
                customerEvent.setContactId(stakeholderDto.getId());
                customerEvent.setType("2");
                customerEvent.setEvent(event);
                customerEvent.setEventDate(eventDateArray[index]);
                customerEventDao.save(customerEvent);
                index++;
            }
        }
        return stakeholderDto;
    }

    @Override
    public int count(Map<String,Object> params) {
        return customContactStakeholderDao.count(params);
    }

    @Override
    public List<CustomContactStakeholderDto> list(Map<String,Object> params, Integer offset, Integer limit) {
        List<CustomContactStakeholder> stakeholderList = customContactStakeholderDao.list(params, offset, limit);
        List<CustomContactStakeholderDto> stakeholderDtoList = new ArrayList<CustomContactStakeholderDto>();
        if (!stakeholderList.isEmpty()){
            for (CustomContactStakeholder stakeholder : stakeholderList) {
                List<CustomerEvent> eventList = customerEventDao.findListByHolderId(stakeholder.getId());
                CustomContactStakeholderDto stakeholderDto = new CustomContactStakeholderDto();
                BeanUtils.copyProperties(stakeholder,stakeholderDto);
//                stakeholderDto.setEventList(eventList);
                String event[] = new String[eventList.size()];
                String eventDate[] = new String[eventList.size()];
                if (!eventList.isEmpty()){
                    int index = 0;
                    for (CustomerEvent customerEvent : eventList) {
                        event[index] = customerEvent.getEvent();
                        eventDate[index] = customerEvent.getEventDate();
                        index++;
                    }
                }
                stakeholderDto.setEvent(event);
                stakeholderDto.setEventDate(eventDate);
                stakeholderDtoList.add(stakeholderDto);
            }
        }
        return stakeholderDtoList;
    }

    @Transactional
    @Override
    public void delete(Long id) {
        customContactStakeholderDao.delete(id);
        customerEventDao.deleteEventByContactId(id);
    }
}
